<html>
<!-- =====================================================================

  File:      Generated file for Adventure Works Cycles Storefront Sample
  Summary:   Self-documentation for application
  Date:	     June 16, 2003

=====================================================================

  This file is part of the Microsoft SQL Server Code Samples.
  Copyright (C) 2003 Microsoft Corporation.  All rights reserved.

This source code is intended only as a supplement to Microsoft
Development Tools and/or on-line documentation.  See these other
materials for detailed information regarding Microsoft code samples.

THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.

======================================================= -->
<head>
  <link rel=stylesheet href=style.css>
</head>
<body>
<div class=SourcePanel style="font-size:12">
<pre style="background-color:white">
<span style="color: #0000FF"><strong>using</strong></span> <span style="color: #2040a0">System</span><span style="color: #4444FF">;</span>
<span style="color: #0000FF"><strong>using</strong></span> <span style="color: #2040a0">System</span>.<span style="color: #2040a0">Globalization</span><span style="color: #4444FF">;</span>
<span style="color: #0000FF"><strong>using</strong></span> <span style="color: #2040a0">System</span>.<span style="color: #2040a0">Data</span>.<span style="color: #2040a0">Sql</span><span style="color: #4444FF">;</span>
<span style="color: #0000FF"><strong>using</strong></span> <span style="color: #2040a0">System</span>.<span style="color: #2040a0">Data</span>.<span style="color: #2040a0">SqlTypes</span><span style="color: #4444FF">;</span>

<span style="color: #008000">/*=====================================================================

  File:      Currency.cs for Adventure Works Cycles SQLCLR Layer Sample
  Summary:   Defines a class for handing particular amounts of money in a 
			 particular culture's monetary system.  This class is exposed as 
			 a SQL Server UDT.
  Date:	     August 15, 2003

---------------------------------------------------------------------

  This file is part of the Microsoft SQL Server Code Samples.
  Copyright (C) Microsoft Corporation.  All rights reserved.

This source code is intended only as a supplement to Microsoft
Development Tools and/or on-line documentation.  See these other
materials for detailed information regarding Microsoft code samples.

THIS CODE AND INFORMATION ARE PROVIDED &quot;AS IS&quot; WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.

======================================================= */</span>

<span style="color: #0000FF"><strong>namespace</strong></span> <span style="color: #2040a0">Microsoft</span>.<span style="color: #2040a0">Samples</span>.<span style="color: #2040a0">SqlServer</span>
<span style="color: #4444FF"><strong>{</strong></span>
	<span style="color: #008000">/// &lt;summary&gt;</span>
	<span style="color: #008000">///		Defines a class for handing particular amounts of money in a </span>
	<span style="color: #008000">///		particular culture's monetary system.  This class is exposed as </span>
	<span style="color: #008000">///		a SQL Server UDT.</span>
	<span style="color: #008000">/// &lt;/summary&gt;</span>
	<span style="color: #4444FF">[</span><span style="color: #2040a0">Serializable</span><span style="color: #4444FF">]</span>
	<span style="color: #4444FF">[</span><span style="color: #2040a0">SqlUserDefinedType</span><span style="color: #4444FF">(</span><span style="color: #2040a0">Format</span>.<span style="color: #2040a0">UserDefined</span>, <span style="color: #2040a0">IsByteOrdered</span> <span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>true</strong></span>, <span style="color: #2040a0">MaxByteSize</span> <span style="color: #4444FF">=</span> <span style="color: #FF0000">32</span><span style="color: #4444FF">)</span><span style="color: #4444FF">]</span>
	<span style="color: #4444FF">[</span><span style="color: #2040a0">CLSCompliant</span><span style="color: #4444FF">(</span><span style="color: #0000FF"><strong>false</strong></span><span style="color: #4444FF">)</span><span style="color: #4444FF">]</span>
	<span style="color: #0000FF"><strong>public</strong></span> <span style="color: #0000FF"><strong>struct</strong></span> <span style="color: #2040a0">Currency</span><span style="color: #4444FF">:</span> <span style="color: #2040a0">INullable</span>, <span style="color: #2040a0">IComparable</span>, <span style="color: #2040a0">IBinarySerialize</span>
	<span style="color: #4444FF"><strong>{</strong></span>
		<span style="color: #0000FF"><strong>const</strong></span> <span style="color: #2040a0">string</span> <span style="color: #2040a0">nullMarker</span> <span style="color: #4444FF">=</span> <span style="color: #444444">&quot;<span style="color: #77dd77">\0</span><span style="color: #77dd77">\0</span><span style="color: #77dd77">\0</span><span style="color: #77dd77">\0</span><span style="color: #77dd77">\0</span><span style="color: #77dd77">\0</span><span style="color: #77dd77">\0</span><span style="color: #77dd77">\0</span><span style="color: #77dd77">\0</span><span style="color: #77dd77">\0</span>&quot;</span><span style="color: #4444FF">;</span>
		<span style="color: #0000FF"><strong>const</strong></span> <span style="color: #0000FF"><strong>int</strong></span> <span style="color: #2040a0">cultureNameMaxSize</span> <span style="color: #4444FF">=</span> <span style="color: #FF0000">10</span><span style="color: #4444FF">;</span>

		<span style="color: #0000FF"><strong>private</strong></span> <span style="color: #2040a0">string</span> <span style="color: #2040a0">cultureName</span><span style="color: #4444FF">;</span>		<span style="color: #008000">//Who issued the money (en-us, for example)</span>

		<span style="color: #0000FF"><strong>private</strong></span> <span style="color: #2040a0">CultureInfo</span> <span style="color: #2040a0">culture</span><span style="color: #4444FF">;</span>	<span style="color: #008000">//The object which represents cultureName</span>

		<span style="color: #0000FF"><strong>private</strong></span> <span style="color: #2040a0">decimal</span> <span style="color: #2040a0">currencyValue</span><span style="color: #4444FF">;</span>	<span style="color: #008000">//The amount of money</span>

		<span style="color: #008000">// Public properties for private fields</span>
		<span style="color: #0000FF"><strong>public</strong></span> <span style="color: #2040a0">CultureInfo</span> <span style="color: #2040a0">Culture</span>
		<span style="color: #4444FF"><strong>{</strong></span>
			<span style="color: #2040a0">get</span>
			<span style="color: #4444FF"><strong>{</strong></span>
				<span style="color: #008000">//A culture name is required.  If not present the entire object is considered null.</span>
				<span style="color: #0000FF"><strong>if</strong></span> <span style="color: #4444FF">(</span><span style="color: #2040a0">cultureName</span> <span style="color: #4444FF">=</span><span style="color: #4444FF">=</span> <span style="color: #2040a0">null</span><span style="color: #4444FF">)</span> <span style="color: #0000FF"><strong>return</strong></span> <span style="color: #2040a0">null</span><span style="color: #4444FF">;</span>

				<span style="color: #008000">//If we've got a cached copy of the culture return it.</span>
				<span style="color: #0000FF"><strong>if</strong></span> <span style="color: #4444FF">(</span><span style="color: #2040a0">culture</span> <span style="color: #4444FF">!</span><span style="color: #4444FF">=</span> <span style="color: #2040a0">null</span><span style="color: #4444FF">)</span> <span style="color: #0000FF"><strong>return</strong></span> <span style="color: #2040a0">culture</span><span style="color: #4444FF">;</span>

				<span style="color: #008000">//Otherwise, set the cache and return the culture for the culture name specified.</span>
				<span style="color: #2040a0">culture</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">CultureInfo</span>.<span style="color: #2040a0">CreateSpecificCulture</span><span style="color: #4444FF">(</span><span style="color: #2040a0">cultureName</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
				<span style="color: #0000FF"><strong>return</strong></span> <span style="color: #2040a0">culture</span><span style="color: #4444FF">;</span>
			<span style="color: #4444FF"><strong>}</strong></span>
		<span style="color: #4444FF"><strong>}</strong></span>

		<span style="color: #008000">// Public propertie for the private field.</span>
		<span style="color: #0000FF"><strong>public</strong></span> <span style="color: #2040a0">decimal</span> <span style="color: #2040a0">CurrencyValue</span>
		<span style="color: #4444FF"><strong>{</strong></span>
			<span style="color: #2040a0">get</span>
			<span style="color: #4444FF"><strong>{</strong></span>
				<span style="color: #0000FF"><strong>return</strong></span> <span style="color: #2040a0">currencyValue</span><span style="color: #4444FF">;</span>
			<span style="color: #4444FF"><strong>}</strong></span>
		<span style="color: #4444FF"><strong>}</strong></span>

		<span style="color: #008000">// Constructors for when we have the culture or the name of the culture</span>

		<span style="color: #0000FF"><strong>public</strong></span> <span style="color: #2040a0">Currency</span><span style="color: #4444FF">(</span><span style="color: #2040a0">CultureInfo</span> <span style="color: #2040a0">culture</span>, <span style="color: #2040a0">decimal</span> <span style="color: #2040a0">currencyValue</span><span style="color: #4444FF">)</span>
		<span style="color: #4444FF"><strong>{</strong></span>
			<span style="color: #0000FF"><strong>this</strong></span>.<span style="color: #2040a0">cultureName</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">culture</span>.<span style="color: #2040a0">Name</span><span style="color: #4444FF">;</span>
			<span style="color: #0000FF"><strong>this</strong></span>.<span style="color: #2040a0">culture</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">culture</span><span style="color: #4444FF">;</span>
			<span style="color: #0000FF"><strong>this</strong></span>.<span style="color: #2040a0">currencyValue</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">currencyValue</span><span style="color: #4444FF">;</span>
		<span style="color: #4444FF"><strong>}</strong></span>
		<span style="color: #0000FF"><strong>public</strong></span> <span style="color: #2040a0">Currency</span><span style="color: #4444FF">(</span><span style="color: #2040a0">string</span> <span style="color: #2040a0">cultureName</span>, <span style="color: #2040a0">decimal</span> <span style="color: #2040a0">currencyValue</span><span style="color: #4444FF">)</span>
		<span style="color: #4444FF"><strong>{</strong></span>
			<span style="color: #0000FF"><strong>this</strong></span>.<span style="color: #2040a0">cultureName</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">cultureName</span><span style="color: #4444FF">;</span>
			<span style="color: #0000FF"><strong>this</strong></span>.<span style="color: #2040a0">culture</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">null</span><span style="color: #4444FF">;</span>
			<span style="color: #0000FF"><strong>this</strong></span>.<span style="color: #2040a0">currencyValue</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">currencyValue</span><span style="color: #4444FF">;</span>
		<span style="color: #4444FF"><strong>}</strong></span>

		<span style="color: #008000">//Return the string representation for the currency, including the currency symbol.</span>
		<span style="color: #4444FF">[</span><span style="color: #2040a0">SqlMethod</span><span style="color: #4444FF">(</span><span style="color: #2040a0">IsDeterministic</span> <span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>true</strong></span>, <span style="color: #2040a0">IsPrecise</span> <span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>true</strong></span>, <span style="color: #2040a0">DataAccess</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">DataAccessKind</span>.<span style="color: #2040a0">None</span>, <span style="color: #2040a0">SystemDataAccess</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">SystemDataAccessKind</span>.<span style="color: #2040a0">None</span><span style="color: #4444FF">)</span><span style="color: #4444FF">]</span>
		<span style="color: #0000FF"><strong>public</strong></span> <span style="color: #2040a0">override</span> <span style="color: #2040a0">string</span> <span style="color: #2040a0">ToString</span><span style="color: #4444FF">(</span><span style="color: #4444FF">)</span>
		<span style="color: #4444FF"><strong>{</strong></span>
			<span style="color: #0000FF"><strong>if</strong></span> <span style="color: #4444FF">(</span><span style="color: #0000FF"><strong>this</strong></span>.<span style="color: #2040a0">Culture</span> <span style="color: #4444FF">=</span><span style="color: #4444FF">=</span> <span style="color: #2040a0">null</span><span style="color: #4444FF">)</span> <span style="color: #0000FF"><strong>return</strong></span> <span style="color: #444444">&quot;null&quot;</span><span style="color: #4444FF">;</span>

			<span style="color: #0000FF"><strong>return</strong></span> <span style="color: #2040a0">String</span>.<span style="color: #2040a0">Format</span><span style="color: #4444FF">(</span><span style="color: #0000FF"><strong>this</strong></span>.<span style="color: #2040a0">Culture</span>, <span style="color: #444444">&quot;{0:c}&quot;</span>, <span style="color: #2040a0">currencyValue</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
		<span style="color: #4444FF"><strong>}</strong></span>

		<span style="color: #008000">//The entire value of the currency is considered null if the culture name is null</span>
		<span style="color: #0000FF"><strong>public</strong></span> <span style="color: #0000FF"><strong>bool</strong></span> <span style="color: #2040a0">IsNull</span>
		<span style="color: #4444FF"><strong>{</strong></span>
			<span style="color: #2040a0">get</span>
			<span style="color: #4444FF"><strong>{</strong></span>
				<span style="color: #0000FF"><strong>return</strong></span> <span style="color: #2040a0">cultureName</span> <span style="color: #4444FF">=</span><span style="color: #4444FF">=</span> <span style="color: #2040a0">null</span><span style="color: #4444FF">;</span>
			<span style="color: #4444FF"><strong>}</strong></span>
		<span style="color: #4444FF"><strong>}</strong></span>

		<span style="color: #008000">//The no-argument constructor makes a null currency.</span>
		<span style="color: #0000FF"><strong>public</strong></span> <span style="color: #0000FF"><strong>static</strong></span> <span style="color: #2040a0">Currency</span> <span style="color: #2040a0">Null</span>
		<span style="color: #4444FF"><strong>{</strong></span>
			<span style="color: #2040a0">get</span>
			<span style="color: #4444FF"><strong>{</strong></span>
				<span style="color: #2040a0">Currency</span> <span style="color: #2040a0">h</span> <span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>new</strong></span> <span style="color: #2040a0">Currency</span><span style="color: #4444FF">(</span><span style="color: #4444FF">(</span><span style="color: #2040a0">String</span><span style="color: #4444FF">)</span><span style="color: #2040a0">null</span>, <span style="color: #FF0000">0</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

				<span style="color: #0000FF"><strong>return</strong></span> <span style="color: #2040a0">h</span><span style="color: #4444FF">;</span>
			<span style="color: #4444FF"><strong>}</strong></span>
		<span style="color: #4444FF"><strong>}</strong></span>

		<span style="color: #008000">//Be sure to set the current UI culture before using this method! Even better, provide the culture</span>
		<span style="color: #008000">//specifically (for the method after this one).</span>
		<span style="color: #4444FF">[</span><span style="color: #2040a0">SqlMethod</span><span style="color: #4444FF">(</span><span style="color: #2040a0">IsDeterministic</span> <span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>true</strong></span>, <span style="color: #2040a0">IsPrecise</span> <span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>true</strong></span>, <span style="color: #2040a0">DataAccess</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">DataAccessKind</span>.<span style="color: #2040a0">None</span>, <span style="color: #2040a0">SystemDataAccess</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">SystemDataAccessKind</span>.<span style="color: #2040a0">None</span><span style="color: #4444FF">)</span><span style="color: #4444FF">]</span>
		<span style="color: #0000FF"><strong>public</strong></span> <span style="color: #0000FF"><strong>static</strong></span> <span style="color: #2040a0">Currency</span> <span style="color: #2040a0">Parse</span> <span style="color: #4444FF">(</span> <span style="color: #2040a0">SqlString</span> <span style="color: #2040a0">s</span> <span style="color: #4444FF">)</span>
		<span style="color: #4444FF"><strong>{</strong></span>
			<span style="color: #0000FF"><strong>return</strong></span> <span style="color: #2040a0">ParseWithCulture</span><span style="color: #4444FF">(</span><span style="color: #2040a0">s</span>, <span style="color: #2040a0">CultureInfo</span>.<span style="color: #2040a0">CurrentUICulture</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
		<span style="color: #4444FF"><strong>}</strong></span>

		<span style="color: #0000FF"><strong>public</strong></span> <span style="color: #0000FF"><strong>static</strong></span> <span style="color: #2040a0">Currency</span> <span style="color: #2040a0">ParseWithCulture</span><span style="color: #4444FF">(</span><span style="color: #2040a0">SqlString</span> <span style="color: #2040a0">s</span>, <span style="color: #2040a0">CultureInfo</span> <span style="color: #2040a0">culture</span><span style="color: #4444FF">)</span>
		<span style="color: #4444FF"><strong>{</strong></span>
			<span style="color: #0000FF"><strong>if</strong></span> <span style="color: #4444FF">(</span><span style="color: #2040a0">s</span>.<span style="color: #2040a0">IsNull</span> <span style="color: #4444FF">|</span><span style="color: #4444FF">|</span> <span style="color: #2040a0">s</span>.<span style="color: #2040a0">Value</span>.<span style="color: #2040a0">ToLower</span><span style="color: #4444FF">(</span><span style="color: #2040a0">CultureInfo</span>.<span style="color: #2040a0">InvariantCulture</span><span style="color: #4444FF">)</span> <span style="color: #4444FF">=</span><span style="color: #4444FF">=</span> <span style="color: #444444">&quot;null&quot;</span><span style="color: #4444FF">)</span>
				<span style="color: #0000FF"><strong>return</strong></span> <span style="color: #2040a0">Currency</span>.<span style="color: #2040a0">Null</span><span style="color: #4444FF">;</span>

			<span style="color: #0000FF"><strong>int</strong></span> <span style="color: #2040a0">digitPos</span> <span style="color: #4444FF">=</span> <span style="color: #4444FF">-</span><span style="color: #FF0000">1</span><span style="color: #4444FF">;</span>
			<span style="color: #2040a0">string</span> <span style="color: #2040a0">stringValue</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">s</span>.<span style="color: #2040a0">Value</span><span style="color: #4444FF">;</span>

			<span style="color: #0000FF"><strong>while</strong></span> <span style="color: #4444FF">(</span><span style="color: #2040a0">digitPos</span> <span style="color: #4444FF">&lt;</span> <span style="color: #2040a0">stringValue</span>.<span style="color: #2040a0">Length</span> <span style="color: #4444FF">&amp;</span><span style="color: #4444FF">&amp;</span> <span style="color: #4444FF">!</span><span style="color: #2040a0">Char</span>.<span style="color: #2040a0">IsDigit</span><span style="color: #4444FF">(</span><span style="color: #2040a0">stringValue</span>, <span style="color: #4444FF">+</span><span style="color: #4444FF">+</span><span style="color: #2040a0">digitPos</span><span style="color: #4444FF">)</span><span style="color: #4444FF">)</span>
			<span style="color: #4444FF"><strong>{</strong></span>
			<span style="color: #4444FF"><strong>}</strong></span>

			<span style="color: #0000FF"><strong>if</strong></span> <span style="color: #4444FF">(</span><span style="color: #2040a0">digitPos</span> <span style="color: #4444FF">&lt;</span> <span style="color: #2040a0">stringValue</span>.<span style="color: #2040a0">Length</span><span style="color: #4444FF">)</span>
				<span style="color: #0000FF"><strong>return</strong></span> <span style="color: #0000FF"><strong>new</strong></span> <span style="color: #2040a0">Currency</span><span style="color: #4444FF">(</span><span style="color: #2040a0">culture</span>, <span style="color: #2040a0">decimal</span>.<span style="color: #2040a0">Parse</span><span style="color: #4444FF">(</span><span style="color: #2040a0">stringValue</span>.<span style="color: #2040a0">Substring</span><span style="color: #4444FF">(</span><span style="color: #2040a0">digitPos</span><span style="color: #4444FF">)</span>, <span style="color: #2040a0">culture</span><span style="color: #4444FF">)</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #0000FF"><strong>return</strong></span> <span style="color: #2040a0">Currency</span>.<span style="color: #2040a0">Null</span><span style="color: #4444FF">;</span>
		<span style="color: #4444FF"><strong>}</strong></span>

		<span style="color: #0000FF"><strong>public</strong></span> <span style="color: #2040a0">override</span> <span style="color: #0000FF"><strong>int</strong></span> <span style="color: #2040a0">GetHashCode</span><span style="color: #4444FF">(</span><span style="color: #4444FF">)</span>
		<span style="color: #4444FF"><strong>{</strong></span>
			<span style="color: #0000FF"><strong>if</strong></span> <span style="color: #4444FF">(</span><span style="color: #0000FF"><strong>this</strong></span>.<span style="color: #2040a0">IsNull</span><span style="color: #4444FF">)</span>
				<span style="color: #0000FF"><strong>return</strong></span> <span style="color: #FF0000">0</span><span style="color: #4444FF">;</span>

			<span style="color: #0000FF"><strong>return</strong></span> <span style="color: #0000FF"><strong>this</strong></span>.<span style="color: #2040a0">ToString</span><span style="color: #4444FF">(</span><span style="color: #4444FF">)</span>.<span style="color: #2040a0">GetHashCode</span><span style="color: #4444FF">(</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
		<span style="color: #4444FF"><strong>}</strong></span>
		<span style="color: #0000FF"><strong>public</strong></span> <span style="color: #0000FF"><strong>int</strong></span> <span style="color: #2040a0">CompareTo</span><span style="color: #4444FF">(</span><span style="color: #2040a0">object</span> <span style="color: #2040a0">other</span><span style="color: #4444FF">)</span>
		<span style="color: #4444FF"><strong>{</strong></span>
			<span style="color: #0000FF"><strong>if</strong></span> <span style="color: #4444FF">(</span><span style="color: #2040a0">other</span> <span style="color: #4444FF">=</span><span style="color: #4444FF">=</span> <span style="color: #2040a0">null</span><span style="color: #4444FF">)</span>
				<span style="color: #0000FF"><strong>return</strong></span> <span style="color: #FF0000">1</span><span style="color: #4444FF">;</span> <span style="color: #008000">//by definition</span>

			<span style="color: #0000FF"><strong>if</strong></span> <span style="color: #4444FF">(</span><span style="color: #2040a0">other</span> <span style="color: #4444FF">=</span><span style="color: #4444FF">=</span> <span style="color: #2040a0">null</span> <span style="color: #4444FF">|</span><span style="color: #4444FF">|</span> <span style="color: #4444FF">!</span><span style="color: #4444FF">(</span><span style="color: #2040a0">other</span> <span style="color: #2040a0">is</span> <span style="color: #2040a0">Currency</span><span style="color: #4444FF">)</span><span style="color: #4444FF">)</span>
				<span style="color: #0000FF"><strong>throw</strong></span> <span style="color: #0000FF"><strong>new</strong></span> <span style="color: #2040a0">ArgumentException</span><span style="color: #4444FF">(</span>
					<span style="color: #444444">&quot;the argument to compare is not a Currency&quot;</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #2040a0">Currency</span> <span style="color: #2040a0">c</span> <span style="color: #4444FF">=</span> <span style="color: #4444FF">(</span><span style="color: #2040a0">Currency</span><span style="color: #4444FF">)</span><span style="color: #2040a0">other</span><span style="color: #4444FF">;</span>

			<span style="color: #0000FF"><strong>if</strong></span> <span style="color: #4444FF">(</span><span style="color: #0000FF"><strong>this</strong></span>.<span style="color: #2040a0">IsNull</span><span style="color: #4444FF">)</span>
			<span style="color: #4444FF"><strong>{</strong></span>
				<span style="color: #0000FF"><strong>if</strong></span> <span style="color: #4444FF">(</span><span style="color: #2040a0">c</span>.<span style="color: #2040a0">IsNull</span><span style="color: #4444FF">)</span>
					<span style="color: #0000FF"><strong>return</strong></span> <span style="color: #FF0000">0</span><span style="color: #4444FF">;</span>

				<span style="color: #0000FF"><strong>return</strong></span> <span style="color: #4444FF">-</span><span style="color: #FF0000">1</span><span style="color: #4444FF">;</span>
			<span style="color: #4444FF"><strong>}</strong></span>

			<span style="color: #0000FF"><strong>if</strong></span> <span style="color: #4444FF">(</span><span style="color: #2040a0">c</span>.<span style="color: #2040a0">IsNull</span><span style="color: #4444FF">)</span>
				<span style="color: #0000FF"><strong>return</strong></span> <span style="color: #FF0000">1</span><span style="color: #4444FF">;</span>

            <span style="color: #2040a0">string</span> <span style="color: #2040a0">thisCultureName</span> <span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>this</strong></span>.<span style="color: #2040a0">Culture</span>.<span style="color: #2040a0">Name</span><span style="color: #4444FF">;</span>
            <span style="color: #2040a0">string</span> <span style="color: #2040a0">otherCultureName</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">c</span>.<span style="color: #2040a0">Culture</span>.<span style="color: #2040a0">Name</span><span style="color: #4444FF">;</span>
            <span style="color: #0000FF"><strong>if</strong></span> <span style="color: #4444FF">(</span><span style="color: #4444FF">!</span><span style="color: #2040a0">thisCultureName</span>.<span style="color: #2040a0">Equals</span><span style="color: #4444FF">(</span><span style="color: #2040a0">otherCultureName</span><span style="color: #4444FF">)</span><span style="color: #4444FF">)</span>
                <span style="color: #0000FF"><strong>return</strong></span> <span style="color: #2040a0">thisCultureName</span>.<span style="color: #2040a0">CompareTo</span><span style="color: #4444FF">(</span><span style="color: #2040a0">otherCultureName</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
            <span style="color: #0000FF"><strong>return</strong></span> <span style="color: #0000FF"><strong>this</strong></span>.<span style="color: #2040a0">CurrencyValue</span>.<span style="color: #2040a0">CompareTo</span><span style="color: #4444FF">(</span><span style="color: #2040a0">c</span>.<span style="color: #2040a0">CurrencyValue</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
        <span style="color: #4444FF"><strong>}</strong></span>
		<span style="color: #008000">// IBinarySerialize methods</span>
		<span style="color: #008000">// The binary layout is as follow:</span>
		<span style="color: #008000">//    Bytes 0 - 19:	Culture name, padded to the right with null characters, UTF-16 encoded</span>
		<span style="color: #008000">//    Bytes 20+:	Decimal value of money</span>
		<span style="color: #008000">// If the culture name is empty, the currency is null.</span>
		<span style="color: #0000FF"><strong>public</strong></span> <span style="color: #0000FF"><strong>void</strong></span> <span style="color: #2040a0">Write</span><span style="color: #4444FF">(</span><span style="color: #2040a0">System</span>.<span style="color: #2040a0">IO</span>.<span style="color: #2040a0">BinaryWriter</span> <span style="color: #2040a0">w</span><span style="color: #4444FF">)</span>
		<span style="color: #4444FF"><strong>{</strong></span>
			<span style="color: #0000FF"><strong>if</strong></span> <span style="color: #4444FF">(</span><span style="color: #0000FF"><strong>this</strong></span>.<span style="color: #2040a0">IsNull</span><span style="color: #4444FF">)</span>
			<span style="color: #4444FF"><strong>{</strong></span>
				<span style="color: #2040a0">w</span>.<span style="color: #2040a0">Write</span><span style="color: #4444FF">(</span><span style="color: #2040a0">nullMarker</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
				<span style="color: #2040a0">w</span>.<span style="color: #2040a0">Write</span><span style="color: #4444FF">(</span><span style="color: #4444FF">(</span><span style="color: #2040a0">decimal</span><span style="color: #4444FF">)</span><span style="color: #FF0000">0</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
				<span style="color: #0000FF"><strong>return</strong></span><span style="color: #4444FF">;</span>
			<span style="color: #4444FF"><strong>}</strong></span>

			<span style="color: #0000FF"><strong>if</strong></span> <span style="color: #4444FF">(</span><span style="color: #2040a0">cultureName</span>.<span style="color: #2040a0">Length</span> <span style="color: #4444FF">&gt;</span> <span style="color: #2040a0">cultureNameMaxSize</span><span style="color: #4444FF">)</span>
				<span style="color: #0000FF"><strong>throw</strong></span> <span style="color: #0000FF"><strong>new</strong></span> <span style="color: #2040a0">ApplicationException</span><span style="color: #4444FF">(</span><span style="color: #2040a0">string</span>.<span style="color: #2040a0">Format</span><span style="color: #4444FF">(</span><span style="color: #2040a0">CultureInfo</span>.<span style="color: #2040a0">InvariantCulture</span>, <span style="color: #444444">&quot;{0} is an invalid culture name for currency as it is too long.&quot;</span>, <span style="color: #2040a0">cultureNameMaxSize</span><span style="color: #4444FF">)</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
			<span style="color: #2040a0">String</span> <span style="color: #2040a0">paddedName</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">cultureName</span>.<span style="color: #2040a0">PadRight</span><span style="color: #4444FF">(</span><span style="color: #2040a0">cultureNameMaxSize</span>, <span style="color: #444444">'<span style="color: #77dd77">\0</span>'</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
			<span style="color: #0000FF"><strong>for</strong></span> <span style="color: #4444FF">(</span><span style="color: #0000FF"><strong>int</strong></span> <span style="color: #2040a0">i</span> <span style="color: #4444FF">=</span> <span style="color: #FF0000">0</span><span style="color: #4444FF">;</span> <span style="color: #2040a0">i</span> <span style="color: #4444FF">&lt;</span> <span style="color: #2040a0">cultureNameMaxSize</span><span style="color: #4444FF">;</span> <span style="color: #2040a0">i</span><span style="color: #4444FF">+</span><span style="color: #4444FF">+</span><span style="color: #4444FF">)</span>
			<span style="color: #4444FF"><strong>{</strong></span>
				<span style="color: #2040a0">w</span>.<span style="color: #2040a0">Write</span><span style="color: #4444FF">(</span><span style="color: #2040a0">paddedName</span><span style="color: #4444FF">[</span><span style="color: #2040a0">i</span><span style="color: #4444FF">]</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
			<span style="color: #4444FF"><strong>}</strong></span>
			<span style="color: #008000">// Normalize decimal value to two places</span>
			<span style="color: #2040a0">currencyValue</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">Decimal</span>.<span style="color: #2040a0">Floor</span><span style="color: #4444FF">(</span><span style="color: #2040a0">currencyValue</span> <span style="color: #4444FF">*</span> <span style="color: #FF0000">100</span><span style="color: #4444FF">)</span> / <span style="color: #FF0000">100</span><span style="color: #4444FF">;</span> 
			<span style="color: #2040a0">w</span>.<span style="color: #2040a0">Write</span><span style="color: #4444FF">(</span><span style="color: #2040a0">currencyValue</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
		<span style="color: #4444FF"><strong>}</strong></span>
		<span style="color: #0000FF"><strong>public</strong></span> <span style="color: #0000FF"><strong>void</strong></span> <span style="color: #2040a0">Read</span><span style="color: #4444FF">(</span><span style="color: #2040a0">System</span>.<span style="color: #2040a0">IO</span>.<span style="color: #2040a0">BinaryReader</span> <span style="color: #2040a0">r</span><span style="color: #4444FF">)</span>
		<span style="color: #4444FF"><strong>{</strong></span>
			<span style="color: #0000FF"><strong>char</strong></span><span style="color: #4444FF">[</span><span style="color: #4444FF">]</span> <span style="color: #2040a0">name</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">r</span>.<span style="color: #2040a0">ReadChars</span><span style="color: #4444FF">(</span><span style="color: #2040a0">cultureNameMaxSize</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
			<span style="color: #0000FF"><strong>int</strong></span> <span style="color: #2040a0">stringEnd</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">Array</span>.<span style="color: #2040a0">IndexOf</span><span style="color: #4444FF">(</span><span style="color: #2040a0">name</span>, <span style="color: #444444">'<span style="color: #77dd77">\0</span>'</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #0000FF"><strong>if</strong></span> <span style="color: #4444FF">(</span><span style="color: #2040a0">stringEnd</span> <span style="color: #4444FF">=</span><span style="color: #4444FF">=</span> <span style="color: #FF0000">0</span><span style="color: #4444FF">)</span>
			<span style="color: #4444FF"><strong>{</strong></span>
				<span style="color: #2040a0">cultureName</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">null</span><span style="color: #4444FF">;</span>
				<span style="color: #0000FF"><strong>return</strong></span><span style="color: #4444FF">;</span>
			<span style="color: #4444FF"><strong>}</strong></span>

			<span style="color: #2040a0">cultureName</span> <span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>new</strong></span> <span style="color: #2040a0">String</span><span style="color: #4444FF">(</span><span style="color: #2040a0">name</span>, <span style="color: #FF0000">0</span>, <span style="color: #2040a0">stringEnd</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
			<span style="color: #2040a0">currencyValue</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">r</span>.<span style="color: #2040a0">ReadDecimal</span><span style="color: #4444FF">(</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
		<span style="color: #4444FF"><strong>}</strong></span>
	<span style="color: #4444FF"><strong>}</strong></span>
<span style="color: #4444FF"><strong>}</strong></span>

</pre>
